//
//  DataManager.h
//  PurchasingMeeting
//
//  Created by zhuang yihang on 7/10/13.
//  Copyright (c) 2013 YX. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "ProductObject.h"
#import "KeyObject.h"
#import "OrderObject.h"
#import "ShopObject.h"
#import "UserObject.h"
#import "SuitObject.h"

typedef void (^DMCompletion)(id response);

typedef void (^DateCompletion)(id response);

typedef void (^block)(void);

/*
 该类封装了商品，种类，类别的读取，保存操作
 */

@interface DataManager : NSObject

+ (DataManager *)sharedManager;

//获取关键字字段信息
- (NSArray *)getKeyInfo;

//返回关键字的分类
- (NSArray *)getKeyCategories;

//返回属于某类别的所有关键字
- (NSMutableArray *)getKeyObjects:(int)categoryCode;

//返回以keyID为父类的keyobject的数组
- (NSMutableArray *)getChildrenKeyObjects:(int)keyID;

//返回指定类别的父类，如果没有，返回-1
- (int)getParentCategoryCode:(int)categoryCode;

//返回属于同一产品编码的产品
- (NSMutableArray *)getSimiliarProduct:(ProductObject *)product;
//同系列所有产品
- (NSMutableArray *)getAllProduct:(ProductObject *)product;

- (ProductObject *)getProductEx:(NSString *)pCode withColorID:(NSString*)colorid;


//获取加大码，加裤的信息
- (NSDictionary *)getAdditionalProduct:(NSString *)pCode withColorID:(NSString *)colorid;

//根据产品类型和款式，返回产品列表
//page从0开始
- (NSArray *)getProduct:(NSArray *)filters page:(int)page pageSize:(int)pageSize;

//根据产品类型和款式，返回产品列表
//page从0开始
- (NSArray *)getProduct:(NSArray *)filters type:(NSString *)type page:(int)page pageSize:(int)pageSize;

//根据产品类型和款式，返回产品列表
//page从0开始
- (NSArray *)getProduct:(NSArray *)filters type:(NSString *)type style:(NSString *)style page:(int)page pageSize:(int)pageSize;

//获取取消款的订单信息
- (NSArray *)getCanceledOrderProducts:(NSArray*)Keys Shopid:(NSString*)shopid;

//按款式颜色获取产品
- (ProductObject *)getProductObjectByName:(NSString *)pName andColor:(NSString*)color;

//获取产品所有的尺寸列表
//返回数组的元素为SizeObject
- (NSArray *)getSizeListBySizeGroup:(int)sizeGroupID SizeType:(NSString*)sizeType;
//判断尺码是否有版别
- (NSArray *)getSizeTypeBySizeGroup:(int)sizeGroupID;
- (NSArray *)getSpdmOrder;

//根据简码搜索产品
- (NSArray *)searchProductWithSimCode:(NSString *)key;
- (NSString *)searchProductCountWithSimCode:(NSString *)key;

//根据产品编码搜索产品，支持模糊搜索
- (NSArray *)searchProductWithCode:(NSString *)key;

//根据产品名称和颜色，返回产品实例
- (ProductObject *)getProduct:(NSString *)pCode withColorID:(NSString*)colorid;

//////////////////////////////////////////////////////////////////////////////////////////////////////
//返回所有配装数组，数组的元素为SuitObject
- (NSArray *)getSuitArray;

//返回推荐配装列表
- (NSArray *)getRecommendSuitArray;

//返回配装中，某个类型的搭配的其他推荐
-(NSArray *)GetSuitSimilarObject:(NSString *)scripno withType:(NSString *)type;

//根据款号和颜色返回所有配装数组，数组的元素为SuitObject
- (NSArray *)GetSuitArrayByCode:(NSString*)code ColorId:(NSString*)colorid;

//根据款号返回所有配装数组，数组的元素为SuitObject,只包含配装component信息
- (NSArray *)SearchSuitArrayByCode:(NSString*)code;

- (NSArray *)getSuitArrayByFilter:(NSArray*)filters insuit:(BOOL)insuit;
//////////////////////////////////////////////////////////////////////////////////////////////////////
//用户接口

//登录
- (BOOL)login:(NSString *)userCode password:(NSString *)password u_id:(NSString *)u_id;
//登出
- (void)logout;
//获取用户的信息，如果为登录，返回空
- (UserObject *)getUserObject;
//设置用户信息
- (void)setUserObject:(NSString *)usercode name:(NSString *)username u_id:(NSString *)u_id shop:(NSArray *)shopid password:(NSString *)password;

//获取产品的订单信息
//如果之前客户已经订购过该产品，OrderObject 中会包含订购的信息
//请调用OrderObject的getOrder函数来获取下单的数量
- (OrderObject *)createOrderByProduct:(ProductObject *)product ShopId:(NSString*)shopid;

//下订单
- (void)placeOrder:(OrderObject *)order;

//获取用户所拥有的店铺列表，数组的元素是ShopObject
- (NSArray *)getShopList;
- (NSArray *)getWrittenShopList;
- (void)setShopList:(NSArray *)shops;

//获取当前店铺和设置当前店铺信息
- (void)setCurrentShop:(ShopObject*)shop;
- (ShopObject *)getCurrentShop;

- (ShopObject *)getShopObject:(NSString *)usercode;
- (NSArray *)getShopObjects:(NSString *)usercode;
-(NSString *)getUserNameByLocal:(NSString *)usercode;

////
//获取用户收藏的产品列表
//注意：该接口比较耗时，使用的时候应该尽量将返回的结果保存
//如果有新增收藏或取消收藏，再重新调用该接口
- (NSArray *)getFavoriteProducts:(NSArray*)filters Shopid:(NSString*)shopid;

-(NSMutableDictionary *)getFavoriteCount:(NSString *)shopid all:(NSString *)allname;
//设置产品为收藏
- (void)setAsFavorite:(ProductObject *)product withStar:(int)star Shopid:(NSString*)shopid type:(NSString*)type;

//取消产品收藏
- (void)cancelAsFavorite:(ProductObject *)product Shopid:(NSString*)shopid;

//产品是否被收藏,返回收藏的星级，如果没有收藏，返回0
- (int)productIsFavorite:(ProductObject *)product Shopid:(NSString*)shopid;

//获取用户收藏的配装列表
- (NSArray *)getsuitFavoriteProducts:(NSArray*)filters Shopid:(NSString*)shopid;

//设置配装为收藏
- (void)setAssuitFavorite:(SuitObject *)suit withStar:(int)star Shopid:(NSString *)shopid type:(NSString *)type;
//取消配装收藏
- (void)cancelAssuitFavorite:(SuitObject *)suit Shopid:(NSString*)shopid;

//配装是否被收藏,返回收藏的星级，如果没有收藏，返回0
- (int)productIssuitFavorite:(SuitObject *)suit Shopid:(NSString*)shopid;
//////////////////////////////////////////////////////////////////////////////////////////////////////

//服务器同步数据
- (void)updateProductTable:(DMCompletion)completion;
- (void)updateProductColoridTable:(DMCompletion)completion;
- (void)updateProductPictureTable:(DMCompletion)completion;
- (void)updateSpdmStatusTimeTable:(DMCompletion)completion;
- (void)updateSuitPm:(DMCompletion)completion;
- (void)updateOrder:(DMCompletion)completion;
- (void)updateSpdmSize:(DMCompletion)completion;
- (void)updateTip:(DMCompletion)completion;
- (void)updateSpdmOrder:(DMCompletion)completion;
- (void)updateg_Size:(DMCompletion)completion;

- (BOOL)hasUpdate;
- (BOOL)hasTipUpdate;
- (void)performUpdate;
- (void)performTipUpdate;

//////////////////////////////////////////////////////////////////////////////////////////////////////
//新的下单接口

//注意：：
//g_meeting为未上传到服务器上的订单列表

//获取下过订单的产品
//无过滤条件
//- (NSArray *)getOrderedProducts:(NSString*)shopid Submit:(int)flag;

//包含过滤条件
//- (NSArray *)getOrderedProducts:(NSString *)type style:(NSString *)style Shopid:(NSString*)shopid Submit:(int)flag;

//包含过滤条件组
- (NSArray *)getOrderedProducts:(NSArray*)Keys Shopid:(NSString*)shopid Submit:(int)flag;

- (int)getUnloadProduct:(NSString *)shopid;

//下单
//输入：需要购买的商品，尺码编号，数量
- (void)addOrder:(OrderObject *)product withSizeNo:(NSString *)size byAmout:(int)amount Shopid:(NSString*)shopid;

//获取商品的下单数量
- (int)getOrderAmount:(ProductObject *)product Shopid:(NSString*)shopid;

//按款号获取订单数量
- (int)getOrderAmountWithCode:(ProductObject *)product Shopid:(NSString*)shopid;

//根据店铺获取店铺汇总信息，包括订单总数和金额
- (NSMutableDictionary*)getOrderAmount:(NSString*)shopid;


//获取商品某个尺码的下单数量
- (int)getOrderAmountBySizeNo:(ProductObject *)product Shopid:(NSString*)shopid size:(NSString *)sizeNo;

//根据商品获取已经下单的下单信息
-(OrderObject*)getOrderByProduct:(ProductObject*)product Shopid:(NSString*)shopid;

//根据店铺获取该店铺的订单信息 flag=0为未提交订单 flag=1为已提交订单 flag = 2未全部订单
- (NSMutableArray *)getOrdered:(NSString*)shopid Submit:(int)flag;
- (NSMutableArray *)getOrderedWithFilter:(NSString *)shopid Submit:(int)flag filter:(NSArray *)filter;

//获取没有提交的订单
- (NSMutableArray *)getOrderInfo:(NSString*)shopid Submit:(int)flag;

//按款式颜色获取订单 flag=0为未提交订单 flag=1为已提交订单
- (OrderObject *)getOrderInfoByItem:(NSString*)shopid Code:(NSString*)code Colorid:(NSString*)colorid Submit:(int)flag;

- (void)markOrderAsUploaded:(OrderObject *)obj time:(NSString *)upload shop:(ShopObject *)shop;

//清订单功能，将该用户所有订单置为未上传
- (void)clearOrderUptime;

//复制全部订单功能
-(void)Copyorder:(NSString*)usercode code:(NSArray *)code FromShopid:(NSString *)fromShopid ToShopid:(NSString *)toShopid withTime:(int)times;

//////////////////////////////////////////////////////////////////////////////////////////////////////
//统计接口



-(NSMutableArray*)getOrderStat:(KeyObject*)key Shopid:(NSString*)shopid;

//获取报表类型名称
-(NSArray*)getStatType;

-(NSArray*)getOrderReport:(int)reportId Filter:(KeyObject*)key Shopid:(NSString*)shopid;

-(NSArray*)getOrderReportColumnCName:(int)reportId;

-(NSArray*)transReport:(NSDictionary*)dict;


- (NSString *)getUserDesc;


- (BOOL)addUser:(NSString *)usercode
           name:(NSString *)username
       password:(NSString *)password
           u_id:(NSString *)u_id
         shopid:(NSString *)shopID
       shopname:(NSString *)shopname;

//汇总所有订单数据，包括已提交和未提交
- (NSMutableDictionary *)getAllOrderSum:(NSString*)shopid;
//设置并保存配比
-(void)setmatching:(NSString *)type childtype:(NSString *)childtype typeno:(int)typeno num:(NSDictionary *)numdic sizearr:(NSArray *)sizearr;
//获取配比
-(NSArray*)getmatching:(NSString *)type childtype:(NSString *)childtype;
- (void)deleteOrderItem:(OrderObject *)order withSizeNo:(NSString *)size withTableName:(NSString *)tablename Shopid:(NSString*)shopid;

-(void)checkShop:(ShopObject *)obj;

-(void)veriShop:(NSString *)usercode password:(NSString *)password shopid:(NSString *)shopid;

-(void)veriShop:(NSString *)u_id shopid:(NSString *)shopid;

-(void)setnewServerIP:(NSString *)url;
-(NSString *)getnewServerIP;

-(NSArray *)issizecancel:(NSString *)code colorid:(NSString *)colorid;

-(void)deleleremovedproductorder:(NSString *)shopid;

-(NSString *)getgroupno:(NSString *)keyname;

-(void)RunBlockAfterDelay:(NSTimeInterval)delay completion:(block)completion;

-(void)setTipsarray:(NSArray *)array;
-(NSArray *)getTipsarray;
-(void)showtip;

-(NSString *)getspdmorder:(ProductObject *)obj titlename:(NSString *)titlename;
-(int)gettypeno:(NSString *)name;

-(void)deleteuserandshops:(NSString *)usercode password:(NSString *)password;
-(void)adduserandshops:(NSString *)usercode password:(NSString *)password username:(NSString *)username biurl:(NSString *)biurl inputtype:(int)inputtype shops:(NSDictionary *)shops;
-(NSString *)getusername:(NSString *)usercode;
-(NSString *)getbiurl:(NSString *)usercode;
-(NSString *)getinputtype:(NSString *)usercode;
-(BOOL)checkusercode:(NSString *)usercode password:(NSString *)password;
- (BOOL)checkusercodeByID:(NSString *)uID;
- (NSString *)getUserCode:(NSString *)u_id;
-(BOOL)userExist:(NSString *)usercode;

-(void)updateProductstatus:(NSArray *)array;


- (void)forceUpdate:(DMCompletion)completion;

- (ShopObject *)getShopObjectByID:(NSString *)shopid;

-(void)refreshSpdmOrderTable:(DMCompletion)completion;
- (void)deleteTable:(NSString *)tablename;

- (void)deleteTableByShop:(NSString *)tablename shop:(NSString *)shopid;
- (void)resetOrderTable;
- (void)copyDatabase;
-(NSString *)getuuid;

- (void)forceSubmit:(DMCompletion)completion;
- (void)forceSubmitAllOrder:(DMCompletion)completion;

- (NSString *)getSubmitLastTime;

- (NSArray *)getSizeGroup;

- (int)getColumnWidth:(int)reportId column:(int)idx;
- (void)verifyOrderWithServer:(DMCompletion)completion;
- (int)isAddtionProduct:(NSString *)code;
- (NSString *)getNormalProductCode:(NSString *)pCode;

- (NSArray *)getSuitSerialArray;
- (void)clear;
@end
